RULE_MAPPING_GRAPH_TEMPLATES_V1 = """
{
   "rule_code":  "{{ rule_code }}",
	"edges": [
	    {% if attrType == "basicsMulti" or attrType == "classifyMulti" %}
		{
			"id": "1",
			"source": "start",
			"target": "condition"
		},
		{
			"id": "2",
			"source": "condition",
			"target": "result",
			"sourceHandle": "true"
		},
		{
			"id": "3",
			"source": "condition",
			"target": "pass",
			"sourceHandle": "false"
		},
		{
			"id": "4",
			"source": "result",
			"target": "answer"
		}
		{% endif %}
		{% if attrType == "basicsSingle" or attrType == "classifySingle" %}
		{
			"id": "1",
			"source": "start",
			"target": "result"
		},
		{
			"id": "4",
			"source": "result",
			"target": "pass",
			"sourceHandle": "true"
		},
		{
			"id": "5",
			"source": "result",
			"target": "answer",
			"sourceHandle": "false"
		}
		{% endif %}
	],
	"nodes": [
		{
			"data": {
				"type": "start",
				"title": "start"
			},
			"id": "start"
		},
		{
			"data": {
				"title": "{{ rule_code }}",
				"type": "if-else",
				"logical_operator": "{% if conditions_group.logical_operator is defined %}{{ conditions_group.logical_operator }}{% elif conditions_group and conditions_group | length > 0 and (conditions_group | first).logical_operator is defined %}{{ (conditions_group | first).logical_operator }}{% else %}and{% endif %}",
				"conditions": [
                    {% for cond in conditions_group %}
                    {
                        "comparison_operator": "{{ cond.operator }}",
                        "variable_selector": ["start", "{{ cond.field }}"]
                        {% if cond.value %}"value": {{ cond.value | tojson }}{{ ',' if cond.sub_conditions }}{% endif %}
                        {% if cond.sub_conditions %}
                        "sub_variable_condition": {
                            "logical_operator": "{% if cond.sub_conditions.logical_operator is defined %}{{ cond.sub_conditions.logical_operator }}{% elif cond.sub_conditions and (cond.sub_conditions | length) > 0 %}{{ (cond.sub_conditions | first).logical_operator | default('and') }}{% else %}and{% endif %}",
                            "conditions": [
                                {% for sub_cond in cond.sub_conditions %}
                                {
                                    "comparison_operator": "{{ sub_cond.operator }}",
                                    "variable_selector": ["start", "{{ sub_cond.field }}"],
                                    {% if sub_cond.value %}"value": {{ sub_cond.value | tojson }},{% endif %}
                                    "key": "{{ sub_cond.field }}"
                                }{{ ',' if not loop.last }}
                                {% endfor %}
                            ]
                        }
                        {% endif %}
                    }{{ ',' if not loop.last }}
                    {% endfor %}
				]
			},
			"id": "condition"
		},
		{
			"data": {
				"title": "{{ rule_code }}",
				"type": "if-else",
				"logical_operator": "{% if results.logical_operator is defined %}{{ results.logical_operator }}{% elif results and (results | length) > 0 and (results | first).logical_operator is defined %}{{ (results | first).logical_operator }}{% else %}and{% endif %}",
				"conditions": [
                    {% for cond in results %}
                    {
                        "comparison_operator": "{{ cond.operator }}",
                        "variable_selector": ["start", "{{ cond.field }}"]
                        {% if cond.value %}"value": {{ cond.value | tojson }}{{ ',' if  cond.sub_conditions }}{% endif %}
                        {% if cond.sub_conditions %}
                        "sub_variable_condition": {
                        "logical_operator": "{% if cond.sub_conditions.logical_operator is defined %}{{ cond.sub_conditions.logical_operator }}{% elif cond.sub_conditions and (cond.sub_conditions | length) > 0 %}{{ (cond.sub_conditions | first).logical_operator | default('and') }}{% else %}and{% endif %}",
                            "conditions": [
                                {% for sub_cond in cond.sub_conditions %}
                                {
                                    "comparison_operator": "{{ sub_cond.operator }}",
                                    "variable_selector": ["start", "{{ sub_cond.field }}"],
                                     {% if sub_cond.value %}"value": {{ sub_cond.value | tojson }},{% endif %}
                                    "key": "{{ sub_cond.field }}"
                                }{{ ',' if not loop.last }}
                                {% endfor %}
                            ]
                        }
                        {% endif %}
                    }{{ ',' if not loop.last }}
                    {% endfor %}
				]
			},
			"id": "result"
		},
		{
			"data": {
				"title": "{{ rule_code }}",
				"type": "answer",
				"answer": "{{ error_message }}"
			},
			"id": "answer"
		},
		{
			"data": {
				"title": "{{ rule_code }}",
				"type": "answer",
				"answer": "未匹配规则"
			},
			"id": "pass"
		}
	]
}
"""

RULE_RESULT_V1 = """
{
    "rule_code": "{{ rule_code }}",
    "description": "{{ description }}",
    "result": {
          "logical_operator": "{{ results.logical_operator | default('and') }}",
            "action": [
                {% for cond in results %}
                {
                    "operator": "{{ cond.operator }}",
                    "field": "{{ cond.field }}",
                    "value": {{ cond.value | tojson }}
                }{{ ',' if not loop.last }}
                {% endfor %}
            ]
    },
    "error": {
            "error_message": "{{ error_message }}",
            "error_code": "{{ error_code }}"
    }
}
"""

RULE_GRAPH_TEMPLATES = {
    "rule": {
        "graph": {
            "trigger": RULE_MAPPING_GRAPH_TEMPLATES_V1,
            "answer": RULE_RESULT_V1,
        }
    }
}
